package com.ddyx.util;

import java.io.File;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import javax.servlet.http.HttpServletResponse;

public class ExcelUtils {
	
	public static void main(String[] args) {
		//List<List<String>> ll=getfiles("C:\\Users\\Administrator\\Desktop\\5+(3)\\5+\\5折");
		String []ll= {"32","12","ew"};
		StringBuffer sb = new StringBuffer();
		for(int i = 0; i < ll.length; i++){
		 sb. append(ll[i]+",");
		}
		String s = sb.toString();
		System.out.println(s.substring(0,s.length()-1));
		//System.out.println(ll.toString().replaceAll(" ", ""));
	}
	//文件
	@SuppressWarnings("static-access")
	public static List<List<String>> getfil(String savePath) {
		List<List<String>> list=new ArrayList<>();
		File f = new File(savePath);
		//f.isFile()是否是文件
		//f.exists()文件或目录是否存在
		File fa[] = f.listFiles();
		for (int i = 0; i < fa.length; i++) {
			File fs = fa[i];
			String fileName = fs.getName();
			int ii=383+i;
			//int ii=260+i;
			File fas[] =fs.listFiles();
			List<String> ll=new ArrayList<>();
			ll.add(ii+"");
			ll.add(fileName);
			ll.add("http://p1az9gcpw.bkt.clouddn.com/"+fas[0].getName());
			StringBuffer sb = new StringBuffer();
			for(int j = 0; j < fas.length; j++){
				sb.append("http://p1az9gcpw.bkt.clouddn.com/"+fas[j].getName()+",");
			}
			String s = sb.toString();
			ll.add(s.substring(0,s.length()-1));
			ll.add(new DateUtil().getNow());
			list.add(ll);
		}
		return list;
	}
	//文件夹
	public static List<List<String>> getfiles(String savePath) {
		List<List<String>> list=new ArrayList<>();
		File f = new File(savePath);
		//f.isFile()是否是文件
		//f.exists()文件或目录是否存在
		File fa[] = f.listFiles();
		for (int i = 0; i < fa.length; i++) {
			File fs = fa[i];
			String fileName = fs.getName();
			int ii=383+i;
			//int ii=260+i;
			File fas[] =fs.listFiles();
			for (int j = 0; j < fas.length; j++) {
				List<String> ll=new ArrayList<>();
				ll.add(ii+"");
				ll.add(fileName);
				ll.add("http://p1az9gcpw.bkt.clouddn.com/"+fas[j].getName());
				list.add(ll);
			}
		}
		return list;
	}
	/**
	 * 导出excel
	 * @param response
	 * @param titlelist    列名集合
	 * @param contentlist	内容集合
	 * @param excelname		excel名字
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "resource", "deprecation" })
	public static void getExcel(HttpServletResponse response, List<String> titlelist,
			List<List<String>> contentlist,String excelname) throws Exception{
	   
		String date=new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		// 第一步，创建一个webbook，对应一个Excel文件  
		HSSFWorkbook workBook = new HSSFWorkbook();  
	    // 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
	 	HSSFSheet sheet = workBook.createSheet(date);  
	 	// 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
	 	HSSFRow row = sheet.createRow((int) 0);
	 	// 第四步，创建单元格，并设置值表头 设置表头居中  
        HSSFCellStyle style = workBook.createCellStyle();
        style.setWrapText(true);//先设置为自动换行
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直方向上居中对齐
	    
	    //设置第一行为Header  
        HSSFCell cell;  

        if(null!=titlelist && titlelist.size()>0){
        	for (int i = 0; i < titlelist.size(); i++) {
        		String title=titlelist.get(i);
                cell = row.createCell(i);  
                cell.setCellValue(title);
                cell.setCellStyle(style);
			}
        }
        
        if(null!=contentlist && contentlist.size()>0){
        	for (int i = 0; i < contentlist.size(); i++){  
        		row = sheet.createRow((int) i + 1);
                //设置行的高度
                row.setHeightInPoints(40);
        		List<String> list=contentlist.get(i);
            	//设置列的宽度
        		for (int j = 0; j < list.size(); j++) {
        			sheet.setColumnWidth(j,4410);
				}

                // 第四步，创建单元格，并设置值  
                HSSFCell cellz;
                
                for (int h = 0; h < list.size(); h++) {
                	cellz = row.createCell(h);
                    cellz.setCellValue(list.get(h));
                    cellz.setCellStyle(style);
				}
               
            } 
        }
        //通过Response把数据以Excel格式保存  
		response.reset();  
		response.setContentType("application/msexcel;charset=UTF-8");  
		try {  
			response.addHeader("Content-Disposition", "attachment;filename=\""
					+new String((date+"-"+excelname+ ".xls").getBytes("GBK"),"ISO8859_1") + "\"");  
			OutputStream out = response.getOutputStream();  
			workBook.write(out);  
			out.flush();  
			out.close();  
		} catch (Exception e) {  
			e.printStackTrace();  
		}
	}
	
}
